package cn.tedu.mall.front.service.impl;

import cn.tedu.mall.common.exception.CoolSharkServiceException;
import cn.tedu.mall.common.pojo.front.entity.FrontCategoryEntity;
import cn.tedu.mall.common.pojo.front.vo.FrontCategoryTreeVO;
import cn.tedu.mall.common.pojo.product.vo.CategoryStandardVO;
import cn.tedu.mall.common.restful.ResponseCode;
import cn.tedu.mall.front.service.IFrontCategoryService;
import cn.tedu.mall.product.service.ICategoryService;
import cn.tedu.mall.product.service.front.IForFrontCategoryService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * 前台业务层
 * 在这个业务层中,调用一下product(只有product能过获取商品数据)
 * 返回所有分类数据(应该根据一级分类做成分页,返回,因为所有的分类数据量还是比较大的)
 */
@Service
@Slf4j
public class FrontCategoryServiceImpl implements IFrontCategoryService {
    @DubboReference
    private IForFrontCategoryService forFrontCategoryService;
    @Override public FrontCategoryTreeVO categoryTree() {
        /**
         * 1.dubbo调用 mall-product的IForFrontCategoryService
         * 2.mall-product只需要实现返回所有分类list
         * 3.在front中封装三级树
         * 3.1 将所有分类,存放到一个map<Long,List> KEY就是每个分类的parentId,VALUE就是分类对象
         * 3.2 将key=0的取出,获得了一级分类list
         * 3.3 对一级分类做for循环,利用一级分类对象的id获取map的二级分类list
         * 3.5 对二级分类做for循环,利用二级分类对象的id获取map的三级分类list
         * 可以利用这个循环封装好三级分类树
         */
        List<CategoryStandardVO> allCategories=forFrontCategoryService.getAllCategories();
        Map<Long, List<FrontCategoryEntity>> map = initTree(allCategories);
        //获取一级分类数组list
        //测试环境中,数据不全,所以未必1-3级完整的,可能2级下面没有三级,for出错了
        List<FrontCategoryEntity> firstLevelCategoryEntities = map.get(0L);
        if (firstLevelCategoryEntities==null||firstLevelCategoryEntities.size()==0){
            log.info("一级分类空");
            throw new CoolSharkServiceException(ResponseCode.CONFLICT,"一级分类为空");
        }
        for (FrontCategoryEntity oneEntity : firstLevelCategoryEntities) {
            Long oneId = oneEntity.getId();
            List<FrontCategoryEntity> secondLevelCategoryEntities = map.get(oneId);
            if (secondLevelCategoryEntities==null || secondLevelCategoryEntities.size()==0){
                log.info("当前一级分类:{},id:{},对应二级分类为空", oneEntity.getName(),oneEntity.getId());
                continue;
            }
            for (FrontCategoryEntity twoEntity : secondLevelCategoryEntities) {
                Long twoId = twoEntity.getId();
                List<FrontCategoryEntity> thirdLevelCategoryEntieis = map.get(twoId);
                twoEntity.setChildrens(thirdLevelCategoryEntieis);
            }
            oneEntity.setChildrens(secondLevelCategoryEntities);
        }
        FrontCategoryTreeVO tree=new FrontCategoryTreeVO();
        tree.setCategories(firstLevelCategoryEntities);
        return tree;
    }
    private Map<Long,List<FrontCategoryEntity>> initTree(List<CategoryStandardVO> allCategories){
        Map<Long,List<FrontCategoryEntity>> mapCategories=new HashMap<>();
        for (CategoryStandardVO category : allCategories) {
            //转化一下entity
            FrontCategoryEntity entity=new FrontCategoryEntity();
            BeanUtils.copyProperties(category,entity);
            Long parentId = entity.getParentId();
            //判断一下map中存在不存在你这个parentId
            boolean contains = mapCategories.containsKey(parentId);
            //如果不存在这个key,说明这是第一次拿到这个key值,创建对应value
            if(!contains){
                List<FrontCategoryEntity> entities=new ArrayList<>();
                mapCategories.put(parentId,entities);
            }
            mapCategories.get(parentId).add(entity);
        }
        return mapCategories;
    }

    @Override public void deleteCategoryTreeCache() {

    }
}
